// 创建store

//给store去名字的时候一般会使用use开头,后面接上驼峰的名字

import { defineStore } from "pinia"

import { ref, computed } from "vue"

import useUser from "./users"

// const useCounterStore = defineStore('counter', {
//     // 定义state数据
//     state() {
//         return {
//             count: 20,
//             name: '张三'
//         }
//     },
// })

//组合式的写法

const useCounterStore = defineStore('counter', () => {
    //声明状态 state
    let count = ref(778)
    let name = ref('')
    let product = ref([])
    //定义action方法
    let fetchRes = () => {
        fetch("/api/data/index.json")
            .then(body => body.json())
            .then(res => {
                console.log(res)
                product.value = res.data
            })
    }
    //getters 说白了就是计算属性
    let reverseMsg = computed(() => {
        return name.value.split('').reverse().join('')
    })

    //引入其他的store
    let userStore = useUser()

    let trigger = () => {
        // console.log(123)
        userStore.changeUsername()
    }
    return {
        count,
        name,
        fetchRes,
        reverseMsg,
        trigger
    }
}, {
    persist: true
})


export default useCounterStore